package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.DisableNotAllowedException;
import com.sky.exception.EnableNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;


    /**
     * 添加菜品
     *
     * @param dishDTO
     */
    @Override
    public void insertDishWithFlavors(DishDTO dishDTO) {
        //dishDto->dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //补状态
        dish.setStatus(StatusConstant.DISABLE);
        //菜品表 调用DishMapper
        dishMapper.insert(dish);
        //口味数据 调用DishFlavorMapper
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            //遍历所有的口味,并给口味设置 菜品id
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertFlavors(flavors);
        }


    }

    /**
     * 菜品  分页条件查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();

        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        Page<DishVO> pageResult = dishMapper.selectByCondition(dish);

        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return;
        }

        //判断菜品 是否起售 批量判断
        int enableCount = dishMapper.countEnableByIds(ids);

        if (enableCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //判断 id是否被套餐关联  批量判断
        int setMealCount = setmealDishMapper.countByDishIds(ids);
        if (setMealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //批量删除
        dishMapper.deleteByIds(ids);

        //批量删除口味
        dishFlavorMapper.deleteByDishIds(ids);

    }

    /**
     * 根据id查询菜品所有信息
     *
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(Long id) {

        Dish dish = dishMapper.selectById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);

        //从口味表中查出 对应菜品的口味数据
        List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    public void updateDishWithFlavors(DishDTO dishDTO) {
        //
        //dishDto->dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //补状态
        dish.setStatus(StatusConstant.DISABLE);
        //菜品表 调用DishMapper
        dishMapper.update(dish);

        //修改口味数据

        //删除所有口味
        ArrayList<Long> ids = new ArrayList<>();
        ids.add(dish.getId());

        dishFlavorMapper.deleteByDishIds(ids);
        //新增所有口味
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            //遍历所有的口味,并给口味设置 菜品id
            flavors.forEach(flavor -> flavor.setDishId(dishId));
            dishFlavorMapper.insertFlavors(flavors);
        }
    }

    /**
     * 起售禁售菜品
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //判断是 起售 还是停售
        if (status == StatusConstant.ENABLE) {   //起售 菜品的分类必须是 启用状态
            Integer categoryStatus = dishMapper.getCategoryStatusByDishId(id);
            if (categoryStatus == StatusConstant.DISABLE) {
                throw new EnableNotAllowedException(MessageConstant.CATEGORY_DISABLE);
            }
        } else {  //停售  菜品 不能被套餐关联
            ArrayList<Long> ids = new ArrayList<>();
            ids.add(id);
            int setMealCount = setmealDishMapper.countByDishIds(ids);
            if (setMealCount > 0) {
                throw new DisableNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);


    }

    /**
     * 根据分类id查询分类下所有启用的菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.selectByCondition(dish);
        for (DishVO d : dishList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());
            d.setFlavors(flavors);

        }

        return dishList;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();
        return dishOverViewVO;
    }
}
